package com.authorization.project.authorization.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.authorization.common.enums.AuthStatus;
import com.authorization.common.utils.DateUtils;
import com.authorization.common.utils.SecurityUtils;
import com.authorization.common.utils.StringUtils;
import com.authorization.project.authorization.domain.AuthorizationHistory;
import com.authorization.project.authorization.domain.AuthorizationMoudleMappingHistory;
import com.authorization.project.authorization.service.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.authorization.project.authorization.mapper.AuthorizationMapper;
import com.authorization.project.authorization.domain.Authorization;
import org.springframework.transaction.annotation.Transactional;

/**
 * 授权注册信息Service业务层处理
 * 
 * @author 李晓雷
 * @date 2020-04-26
 */
@Service
public class AuthorizationServiceImpl implements IAuthorizationService 
{
    @Autowired
    private AuthorizationMapper authorizationMapper;

    @Autowired
    private IAuthorizationMoudleMappingService iAuthorizationMoudleMappingService;

    @Autowired
    private IMicroserviceService iMicroserviceService;

    @Autowired
    private IAuthorizationHistoryService iAuthorizationHistoryService;

    @Autowired
    private IAuthorizationMoudleMappingHistoryService iAuthorizationMoudleMappingHistoryService;

    /**
     * 查询授权注册信息
     * 
     * @param userid 授权注册信息ID
     * @return 授权注册信息
     */
    @Override
    public Authorization selectAuthorizationById(String userid)
    {
        Authorization authorization = authorizationMapper.selectAuthorizationById(userid);
        return authorization;
    }

    /**
     * 查询授权注册信息列表
     * 
     * @param authorization 授权注册信息
     * @return 授权注册信息
     */
    @Override
    public List<Authorization> selectAuthorizationList(Authorization authorization)
    {
        return authorizationMapper.selectAuthorizationList(authorization);
    }

    /**
     * 新增授权注册信息
     * 
     * @param authorization 授权注册信息
     * @return 结果
     */
    public int insert(Authorization authorization)
    {
        authorization.setAdduser(SecurityUtils.getUsername());
        authorization.setAddtime(DateUtils.getNowDate());
        return authorizationMapper.insertAuthorization(authorization);
    }

    /**
     * 新增授权注册信息
     *
     * @param authorization 授权注册信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAuthorization(Authorization authorization)
    {
        authorization.setStatus(AuthStatus.SUBMIT.getCode());

        //1赋值service字段
        authorization.setServices(iMicroserviceService.getServiceNames(authorization.getModuleArray(), authorization.getProductId()));

        //2添加数据
        int rest = this.insert(authorization);

        //3添加授权-模块中间表
        iAuthorizationMoudleMappingService.batchInsertAuthorizationMoudleMapping(authorization.getUserid(), authorization.getModuleArray(), authorization.getPackageId());
        return rest;
    }

    /**
     * 修改授权注册信息
     * 
     * @param authorization 授权注册信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAuthorization(Authorization authorization)
    {
        //备份数据
        AuthorizationHistory authorizationHistory = new AuthorizationHistory();
        authorizationHistory.setUserid(authorization.getUserid());
        iAuthorizationHistoryService.insertAuthorizationHistory(authorizationHistory);

        //备份中间表
        iAuthorizationMoudleMappingHistoryService.insertAuthorizationMoudleMappingHistoryByUserid(authorization.getUserid(), authorizationHistory.getId());

        //1赋值service字段
        authorization.setServices(iMicroserviceService.getServiceNames(authorization.getModuleArray(), authorization.getProductId()));

        //2删除授权-模块中间表
        iAuthorizationMoudleMappingService.deleteAuthorizationMoudleMappingByUserId(authorization.getUserid());

        //3添加授权-模块中间表
        iAuthorizationMoudleMappingService.batchInsertAuthorizationMoudleMapping(authorization.getUserid(), authorization.getModuleArray(), authorization.getPackageId());

        //4修改数据
        int rest = this.update(authorization);
        return rest;
    }

    /**
     * 修改授权注册信息
     *
     * @param authorization 授权注册信息
     * @return 结果
     */
    public int update(Authorization authorization)
    {
        authorization.setUpdatetime(DateUtils.getNowDate());
        authorization.setUpdateuser(SecurityUtils.getUsername());
        return authorizationMapper.updateAuthorization(authorization);
    }

    /**
     * 批量删除授权注册信息
     * 
     * @param userids 需要删除的授权注册信息ID
     * @return 结果
     */
    @Override
    public int deleteAuthorizationByIds(String[] userids)
    {
        //1删除授权-模块中间表
        for (String userid : userids) {
            iAuthorizationMoudleMappingService.deleteAuthorizationMoudleMappingByUserId(userid);
        }
        return authorizationMapper.deleteAuthorizationByIds(userids);
    }

    /**
     * 删除授权注册信息信息
     * 
     * @param userid 授权注册信息ID
     * @return 结果
     */
    @Override
    public int deleteAuthorizationById(String userid)
    {
        return authorizationMapper.deleteAuthorizationById(userid);
    }

    /**
     * 根据客户编号修改状态
     * @param userid
     * @param status
     * @return
     */
    @Override
    public int updateAuthorizationState(String userid, String status) {
        if(StringUtils.isNotEmpty(userid) && StringUtils.isNotEmpty(status)){
            return authorizationMapper.updateAuthorizationState(userid, status, DateUtils.getNowDate());
        }else {
            return 0;
        }
    }

    /**
     * 通过api添加授权信息
     * @param authorization
     * @return
     */
    @Override
    @Transactional
    public int apiInsertAuthorization(Authorization authorization) {
        authorization.setStatus(AuthStatus.SUBMIT.getCode());
        authorization.setAddtime(DateUtils.getNowDate());
        return authorizationMapper.insertAuthorization(authorization);
    }

    /**
     * 根据客户编号,机器码,token 查询审核状态
     * @param userid
     * @return
     */
    @Override
    public String apiSelstatus(String userid) {
        return authorizationMapper.apiSelstatus(userid);
    }
}
